package com.hqq.web.page;

import com.alibaba.fastjson.JSONObject;
import com.hqq.domain.WoocUser;
import com.hqq.enums.ResultEnum;
import com.hqq.exceptions.InvokeException;
import com.hqq.service.WoocUserService;
import com.hqq.util.Response;
import com.hqq.web.consts.WoocConsts;
import com.hqq.web.util.CookieUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * LoginController
 * Created by heqianqian on 2018/3/25.
 */
@Controller
@RequestMapping("/login")
public class LoginController {
    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private WoocUserService woocUserService;

    /*=============页面相关===========*/

    /**
     * 跳转到登录
     *
     * @return
     */
    @RequestMapping("/login")
    public String index(Model model) {
        model.addAttribute("type", "login");
        return "login/index";
    }

    /**
     * 跳转到注册
     *
     * @return
     */
    @RequestMapping("/register")
    public String register(Model model) {
        model.addAttribute("type", "register");
        return "login/index";
    }


    /*=============接口相关===========*/


    /**
     * 用户注册
     *
     * @param pwd        密码
     * @param verifyCode 验证码
     * @return
     */
    @ResponseBody
    @RequestMapping("/register.ajax")
    public Response register(String userName, String pwd, String verifyCode) {
        logger.debug("===IndexController register===userName:{},pwd:{},verifyCodde:{}", userName, pwd, verifyCode);
        Response response = null;
        try {
            woocUserService.saveUser(userName, pwd);//保存用户信息
            response = Response.from(ResultEnum.SUCCESS.toCode(), ResultEnum.SUCCESS.toDescription());
        } catch (InvokeException e1) {
            logger.error("===IndexController register===", e1);
            response = Response.from(e1.getCode(), e1.getMessage());
        } catch (Exception e) {
            logger.error("===IndexController register===", e);
            response = Response.from(ResultEnum.FAILED.toCode(), ResultEnum.FAILED.toDescription());
        }
        return response;
    }

    /**
     * 登录
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/login.ajax")
    public Response login(String name, String pwd, HttpServletResponse httpServletResponse) {
        logger.debug("===IndexController login===name:{},pwd:{}", name, pwd);
        Response response = null;
        try {
            WoocUser user = woocUserService.checkLogin(name, pwd, null);//不做用户限制
            if (null != user) {//登录成功
                //保存用户信息到cookie
                CookieUtil.addCookie(httpServletResponse, WoocConsts.COOKIE_KEY, JSONObject.toJSONString(user));
                response = Response.from(ResultEnum.SUCCESS.toCode(), ResultEnum.SUCCESS.toDescription(), user);
            } else {
                response = Response.from(ResultEnum.FAILED.toCode(), ResultEnum.FAILED.toDescription());
            }
        } catch (Exception e) {
            logger.error("===IndexController login===", e);
            response = Response.from(ResultEnum.FAILED.toCode(), ResultEnum.FAILED.toDescription());
        }
        return response;
    }

}

